package com.candy.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.demoweb.model.dao.CandyItemDao;

import com.demoweb.model.dto.CandyItem;





@WebServlet("/candy/list.action")
public class ItemListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
//		//로그인 상태가 아닌 경우 로그인 페이지로 이동
		if (request.getSession().getAttribute("loginuser") == null) {
			response.sendRedirect(
				"/demoweb/account/loginform.action?" + 
				"returnuri=" + request.getRequestURI());
			return;
		}
			
//		
//		//데이터 조회 (목록 조회)
//		UploadDao dao = new UploadDao();
//		ArrayList<Upload> uploads = dao.getUploadList();
//		
//		//조회된 목록을 request 객체에 저장
//		request.setAttribute("uploads", uploads);
		
		//데이터 조회 (목록 조회)
		CandyItemDao dao = new CandyItemDao();
		ArrayList<CandyItem> items = (ArrayList<CandyItem>)dao.getItemList();
		
		
		//조회된 목록을 request 객체에 저장
		request.setAttribute("items", items);
		
		//uploadlist.jsp로 forward
		RequestDispatcher dispatcher = 
			request.getRequestDispatcher("/WEB-INF/views/candy/candyitemlist.jsp");

		dispatcher.forward(request, response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
